package com.yeban.base.day06.homework;

import java.util.Scanner;

/**
 * @Desc TODO
 * @Author yeban
 * @Date 2025/10/2
 */
//练习3：实现trim()方法的相同效果
public class StringTest03 {

    public static String trimRealize(String str) {

        char[] arr = str.toCharArray();
        int left = 0;
        int right = arr.length - 1;

        // 找到左边第一个不为空格的字符
        for (int i = 0; i < arr.length; i ++) {
            if(arr[i] != ' ') {
                left = i;
                break;
            }
        }

        // 找到右边第一个不为空格的字符
        for (int i = arr.length - 1; i > 0 ; i --) {
            if(arr[i] != ' ') {
                right = i;
                break;
            }
        }

        String newStr = "";
        for (int i = left; i <= right; i++) {
            newStr += arr[i];
        }

        return newStr;
    }

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.print("请输入一个字符串：");
        String str = sc.nextLine();

        System.out.println(trimRealize(str));

    }
}
